package com.sugar.real.estate.module.business.controller.app.user;

import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.real.estate.module.business.controller.app.user.vo.AppBizUserLoginReqVO;
import com.sugar.real.estate.module.business.controller.app.user.vo.AppBizUserRespVO;
import com.sugar.real.estate.module.business.controller.app.user.vo.AppBizUserUpdateReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.user.BizUserDO;
import com.sugar.real.estate.module.business.service.user.BizUserService;
import com.sugar.real.estate.module.system.api.auth.dto.AppAuthDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 用户")
@RestController
@RequestMapping("/biz/biz-user")
@Validated
public class AppBizUserController {

    @Resource
    private BizUserService bizUserService;

    @PostMapping("/login")
    @Operation(summary = "登录")
    @PermitAll
    public CommonResult<AppAuthDTO> login(@Valid @RequestBody AppBizUserLoginReqVO loginReqVO) {
        return success(bizUserService.login(loginReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    public CommonResult<Boolean> updateBizUser(@Valid @RequestBody AppBizUserUpdateReqVO updateReqVO) {
        bizUserService.appUpdateBizUser(updateReqVO, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得当前用户信息")
    public CommonResult<AppBizUserRespVO> getBizUser() {
        BizUserDO bizUser = bizUserService.getBizUser(SecurityFrameworkUtils.getLoginUserId());
        return success(BeanUtils.toBean(bizUser, AppBizUserRespVO.class));
    }
}